package com.wrupple.muba.catalogs.client.widgets.fields.cells;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.wrupple.muba.catalogs.client.activity.process.task.CatalogInteractionState.Mode;
import com.wrupple.muba.catalogs.client.widgets.fields.cells.templates.ButtonTemplate;
import com.wrupple.muba.catalogs.client.widgets.fields.cells.templates.InlineText;
import com.wrupple.muba.catalogs.client.widgets.impl.CatalogUserInterfaceMessages;
import com.wrupple.muba.common.shared.Process;
import com.wrupple.muba.common.shared.State.ProcessManager;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class MultiTextCell extends ProcessDelegatingEditableField<List<String>> {

	private InlineText inlineText;
	private ButtonTemplate button;
	private CatalogUserInterfaceMessages messages;

	public MultiTextCell(EventBus bus,ProcessManager pm,FieldDescriptor d, Mode mode, Process<List<String>, List<String>> nestedProcess, String nestedProcessLocalizedName) {
		super(bus, pm, nestedProcess,nestedProcessLocalizedName,d, mode);
		inlineText = GWT.create(InlineText.class);
		button = GWT.create(ButtonTemplate.class);
		messages = GWT.create(CatalogUserInterfaceMessages.class);
	}

	@Override
	protected void renderAsInput(com.google.gwt.cell.client.Cell.Context context, List<String> v, SafeHtmlBuilder sb,
			FieldData<List<String>> viewData) {
		if (v != null) {
			renderValues(v, sb);
		}
		sb.append(button.button(messages.addNew()));
	}

	@Override
	protected void renderReadOnly(com.google.gwt.cell.client.Cell.Context context, List<String> v, SafeHtmlBuilder sb,
			FieldData<List<String>> viewData) {
		if (v == null) {
			return;
		}
		if (v != null) {
			renderValues(v, sb);
		}
	}

	@Override
	protected List<String> getCurrentInputValue(Element parent, boolean isEditing) {
		List<String> regreso = new ArrayList<String>();
		NodeList<Node> children = parent.getChildNodes();
		Node temp;
		Node node;
		InputElement input;
		DivElement output;
		String value;
		for (int i = 0; i < children.getLength(); i++) {
			temp = children.getItem(i);
			node = temp.getFirstChild();
			if (isEditing) {
				input = node.cast();
				value = input.getValue();
			} else {
				output = node.cast();
				value = output.getInnerText();
			}
			if (value == null || value.length() == 0) {
			} else {
				regreso.add(value.trim());
			}
		}
		return regreso;
	}

	@Override
	protected void onValueWillCommit(Element parent,
			com.wrupple.muba.catalogs.client.widgets.fields.cells.AbstractEditableField.FieldData<List<String>> viewData) {

	}

	@Override
	protected void onWillEnterEditMode(Element parent,
			com.wrupple.muba.catalogs.client.widgets.fields.cells.AbstractEditableField.FieldData<List<String>> viewData) {

	}


	private void renderValues(List<String> v, SafeHtmlBuilder sb) {
		for (String current : v) {
			sb.appendHtmlConstant("<div>");
			sb.append(inlineText.output(current));
			sb.appendHtmlConstant("</div>");
		}
	}

}
